package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.*;
import java.util.*;

import javax.servlet.http.*;

import models.SolutionSubmission;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

@SuppressWarnings("serial")
public class ajxGetMPSCandidateServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");
		
		boolean bCustomer = false;
		boolean bLocation = false;
		boolean bProcessarea = false;
		boolean bSolType = false;
		boolean bFinBenfit = false;
		boolean bAchBenfit = false;
		boolean bfromDt = false;
		boolean btoDt = false;
		long solID;
		String solCustomer = "";
		String solLocation = "";
		String solProcessarea = "";
		String solSolutiontype = "";
		String solCode =""; //ADDED by Kulprapas SEP 27,2012 - to get Solution code instead Solution ID
		Date solSubmittedON;
		
		String cmbCustomer = req.getParameter("cmbCustomer");
		String cmbLocation = req.getParameter("cmbLocation");
		String cmbProcessarea = req.getParameter("cmbProcessarea");
		String cmbSolType = req.getParameter("cmbSolType");
		String cmbFinBenfit = req.getParameter("cmbFinBenfit");
		String cmbAchBenfit = req.getParameter("cmbAchBenfit");
		String fromDt = req.getParameter("fromDt");
		String toDt = req.getParameter("toDt");	
		if (cmbCustomer != null)
		{
			bCustomer = true;
		}
		if (cmbLocation != null)
		{
			bLocation = true;
		}
		if (cmbProcessarea != null)
		{
			bProcessarea = true;
		}
		if (cmbSolType != null)
		{
			bSolType = true;
		}
		if (cmbFinBenfit != null)
		{
			bFinBenfit = true;
		}
		if (cmbAchBenfit != null)
		{
			bAchBenfit = true;
		}
		if (fromDt != null)
		{
			bfromDt = true;
		}
		if (toDt != null)
		{
			btoDt = true;
		}
		
		String strHTML = "";
		int i = 0;
		boolean ifound = false;
		PrintWriter out = resp.getWriter();

		try {
			
			strHTML = strHTML + "<Table id=\"insured_list\" class=\"tablesorter\" cellpadding=3 width=\"550px\">";
			strHTML = strHTML + "<thead>";
			strHTML = strHTML + "<tr>";
			strHTML = strHTML + "<th></th><th width=\"50px\"><b>Solution ID</th>";
			strHTML = strHTML + "<th width=\"50px\" align=\"center\"><b>Customer </th>";
			strHTML = strHTML + "<th width=\"50px\" align=\"center\"><b>Location </th>";
			strHTML = strHTML + "<th width=\"50px\" align=\"center\"><b>Process Area</th>";
			strHTML = strHTML + "<th width=\"50px\" align=\"center\"><b>Solution Type</th>";
			strHTML = strHTML + "<th width=\"50px\" align=\"center\"><b>Posted Date</th>";
			strHTML = strHTML + "</tr></thead>";
			strHTML = strHTML + "<tbody> ";

			Objectify ofy = ObjectifyService.begin();
			Query<SolutionSubmission> q = ofy.query(SolutionSubmission.class).filter("mpsCandidate", true);
			//Query<SolutionSubmission> q = ofy.query(SolutionSubmission.class);
			if (bCustomer)
			{
				q.filter("Customer", cmbCustomer);
			}
			if (bLocation)
			{
				q.filter("Location", cmbLocation);
			}
			if (bProcessarea)
			{
				q.filter("processArea", cmbProcessarea.replace(" and ", "&"));
			}
			if (bSolType)
			{
				q.filter("solType", cmbSolType.toString().trim());
			}
			if (bFinBenfit)
			{
				if (cmbFinBenfit.toString().trim().equals("Hard Savings")) {
					q.filter("hasHardSavings", true);
				} else if (cmbFinBenfit.toString().trim().equals("Cost Avoidance")) {
					q.filter("hasCostAvoidance", true);
				} else if (cmbFinBenfit.toString().trim().equals("Cost Avoidance")) {
					q.filter("hasOppurtunityCost", true);
				}		
			}
			if (bAchBenfit)
			{
				q.filter("achievedTypeDesc", cmbAchBenfit);
			}
			if (bfromDt)
			{
				Date fdate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(fromDt + " 00:00:01");
				q.filter("submittedON >=", fdate);
			}
			if (btoDt)
			{
				Date tdate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(toDt + " 23:59:59");
				q.filter("submittedON <=", tdate);
			}

			i = 0;
			for (SolutionSubmission f : q.fetch()) {
				solID = f.getSolID();
				solCode =f.getSolCode(); //ADDED by Kulprapas SEP 27,2012 - to get Solution code instead Solution ID
				solLocation = f.getLocation();
				solCustomer = f.getCustomer();
				solSubmittedON = f.getSubmittedON();
				solProcessarea = f.getProcessArea();
				solSolutiontype = f.getSolType();
				
				strHTML = strHTML + "<tr>";
				strHTML = strHTML + "<td width =\"15px\"><input type=\"checkbox\" id=\"chk" + i + " name=\"chk\" onClick='UnSubscribe(" + solID + "," + i + ",this.checked)'></td>";
				strHTML = strHTML + "<td width=\"50px\"><a href='/modules/default/display-solution.jsp?sol_id=" + solID + "' >" + solCode + "</td>";
				strHTML = strHTML + "<td width=\"50px\">" + solCustomer + "</td>";
				strHTML = strHTML + "<td width=\"50px\">" + solLocation + "</td>";
				strHTML = strHTML + "<td width=\"50px\">" + solProcessarea + "</td>";				
				strHTML = strHTML + "<td width=\"50px\">" + solSolutiontype + "</td>";
				strHTML = strHTML + "<td width=\"50px\">" + solSubmittedON + "</td>";
				strHTML = strHTML + "</tr>";
				i++;
				ifound = true;
			}
			strHTML = strHTML + "</tbody> ";
			strHTML = strHTML + "</table>";			
			if (!ifound)
			{
				strHTML = "<center><p style=\"font-family:Times New Roman;font-weight:bold;color:black;font-size:25px\">No Shared MPS Candidate Solutions</p></center>";
			}
			strHTML = strHTML + "<div id='countera' style='display:none'>" + i + "</div>";
			out.println(strHTML);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			//out.println("MyFavouriteServlet:Exception" + e.getMessage());
			//e.printStackTrace();
			strHTML = "<div style='font-family:Arial;font-weight:bold;color:black;font-size:25px'><center>" + e.getMessage() + "</center></div>";
			out.println(strHTML);
		}
	}
}
